package com.ebiz.mockup
{
	import com.ebiz.mockup.core.Core;
	import com.ebiz.mockup.frameworkExtends.pureMVC.AbstractMediator;
	import com.ebiz.mockup.messages.AddComponentInBoardMSG;
	import com.ebiz.mockup.messages.SelectCellMSG;
	import com.ebiz.mockup.view.BoardMediator;
	
	import flash.events.MouseEvent;
	
	import org.puremvc.as3.multicore.interfaces.INotification;
	
	public class MockupCoreMediator extends AbstractMediator
	{
		public static const NAME:String = "MockupCoreMediator";
		
		private function get view():MockupCore{
			return this.viewComponent as MockupCore;
		}
		
		public function MockupCoreMediator(viewComponent:Object=null)
		{
			super(NAME, viewComponent);
			this.notificationInterests = [
				SelectCellMSG.SELECT_LIST_COMPONENT_AND_PREPARE_ADD,
				AddComponentInBoardMSG.STOP_PREPARE_ADD_A_COMPONENT
			];
		}
		
		override public function onRegister():void{
			super.onRegister();
			//register mediator
			this.facade.registerMediator(new BoardMediator(this.view.board));
			this.view.addEventListener(MouseEvent.MOUSE_UP,view_mouseUpHandler);
		}
		
		override public function onRemove():void{
			super.onRemove();
			this.facade.removeMediator((BoardMediator.NAME));
		}
		
		override public function handleNotification(notification:INotification):void{
			switch(notification.getName()){
				case SelectCellMSG.SELECT_LIST_COMPONENT_AND_PREPARE_ADD:
					var $msg:SelectCellMSG = notification.getBody() as SelectCellMSG;
//					if($msg.type=="component"){
						this.view.draggableVisual.source = (notification.getBody() as SelectCellMSG).iconBig;
						Core.instance.dragging = (notification.getBody() as SelectCellMSG).data;
						Core.instance.draggingAlwaysMouseDown = true;
//					}else{
//						this.view.draggableVisual.source = null;
//						Core.instance.dragging = null;
//						Core.instance.draggingAlwaysMouseDown = false;
//					}
					break;
				case AddComponentInBoardMSG.STOP_PREPARE_ADD_A_COMPONENT:
					Core.instance.dragging = null;
					Core.instance.draggingAlwaysMouseDown = false;
					this.view.draggableVisual.source = null;
					break;
			}
		}
		
		private function view_mouseUpHandler(event:MouseEvent):void{
			if(Core.instance.dragging!=null){
				if(this.view.board.hitTestPoint(this.view.mouseX,this.view.mouseY)){
					//mouseUp in Board, do board mouseUpHandler
				}else{
					Core.instance.draggingAlwaysMouseDown = false;
				}
			}
		}
		
	}
}